package com.huawei.hianalytics.util;

import android.text.TextUtils;
import com.huawei.hianalytics.g.b;
import com.tencent.mm.opensdk.constants.ConstantsAPI.AppSupportContentFlag;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

public class g {
    public static int a(int i, int i2, int i3) {
        if (i > i2) {
            b.c("HianalyticsSDK", "checkIntRange(): parameter overlarge.");
            return i2;
        } else if (i >= i3) {
            return i;
        } else {
            b.c("HianalyticsSDK", "checkIntRange(): parameter under size.");
            return i3;
        }
    }

    public static String a(String str, int i, int i2) {
        try {
            int parseInt = Integer.parseInt(str);
            if (parseInt <= i && parseInt >= i2) {
                return String.valueOf(str);
            }
            b.c("HianalyticsSDK", "checkMcc(): MCC out of range!");
            return "";
        } catch (NumberFormatException e) {
            b.c("HianalyticsSDK", "checkMcc(): mcc is not a number!please set up correctly");
            return "";
        }
    }

    public static String a(String str, String str2, String str3, String str4) {
        if (!TextUtils.isEmpty(str2)) {
            return a(str, str2, str3) ? str2 : str4;
        } else {
            b.c("HianalyticsSDK", "checkStrParameter() Parameter verification failure! Parameter:" + str);
            return str4;
        }
    }

    public static Map<String, String> a(Map<String, String> map, int i, long j, long j2, String str) {
        if (map == null || map.size() == 0) {
            b.c("HianalyticsSDK", "checkMap():headers is empty");
            return null;
        } else if (map.size() > i) {
            b.c("HianalyticsSDK", "checkMap(): map size is too big");
            return null;
        } else {
            Map<String, String> hashMap = new HashMap();
            for (Entry entry : map.entrySet()) {
                String str2 = (String) entry.getKey();
                if (TextUtils.isEmpty(str2) || ((long) str2.length()) > j) {
                    b.c("HianalyticsSDK", "checkMap():key check failure");
                } else if (TextUtils.isEmpty(str) || str2.startsWith(str)) {
                    String str3 = (String) entry.getValue();
                    if (TextUtils.isEmpty(str3) || ((long) str3.length()) > j2) {
                        b.c("HianalyticsSDK", "checkMap():value check failure");
                    } else {
                        hashMap.put(str2, entry.getValue());
                    }
                } else {
                    b.c("HianalyticsSDK", "checkMap() key is not starts with x_");
                }
            }
            return hashMap;
        }
    }

    public static boolean a(String str) {
        return !a("eventId", str, 256);
    }

    public static boolean a(String str, String str2, int i) {
        if (TextUtils.isEmpty(str2)) {
            b.c("HianalyticsSDK", "checkString() Parameter is empty : " + str);
            return false;
        } else if (str2.length() <= i) {
            return true;
        } else {
            b.c("HianalyticsSDK", "checkString() Failure of parameter length check! Parameter:" + str);
            return false;
        }
    }

    public static boolean a(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str2)) {
            b.c("HianalyticsSDK", "checkString() Parameter is null! Parameter:" + str);
            return false;
        } else if (Pattern.compile(str3).matcher(str2).matches()) {
            return true;
        } else {
            b.c("HianalyticsSDK", "checkString() Parameter verification failure! Parameter:" + str);
            return false;
        }
    }

    public static boolean a(Map<String, String> map) {
        if (map == null || map.size() == 0) {
            b.c("HianalyticsSDK", "onEvent() mapValue has not data.so,The data will be empty");
            return false;
        } else if (map.size() == 1 && (map.get("constants") != null || map.get("_constants") != null)) {
            b.c("HianalyticsSDK", "checkMap() the key can't be constants or _constants");
            return false;
        } else if (map.size() <= 2048 && map.toString().length() <= 204800) {
            return true;
        } else {
            b.c("HianalyticsSDK", "checkMap Map data is too big! size: %d , length: %d", Integer.valueOf(map.size()), Integer.valueOf(map.toString().length()));
            return false;
        }
    }

    public static Map<String, String> b(Map<String, String> map) {
        return a(map, 10, 128, 512, "x_");
    }

    public static Map<String, String> c(Map<String, String> map) {
        return a(map, 50, AppSupportContentFlag.MMAPP_SUPPORT_XLS, AppSupportContentFlag.MMAPP_SUPPORT_XLS, "x-hasdk");
    }
}
